import { AdTypes } from "./PlatDefine";

export class AdData {
    /* 回调 */
    caller: any = null;
    /**广告显示回调 */
    private _cbShow: Function = null;
    /**广告关闭回调 */
    private _cbClose: Function = null;
    /**广告是否正在显示 调用微信平台show()前面和hide()中函数时更改状态*/
    isShow: boolean = false;
    /**显示成功了，正在显示中 */
    isShowing: boolean = false;
    /**数据 (游戏传过来的数据)*/
    data: any = null;
    /**banner广告在屏幕内还是屏幕外 true:屏幕内 false:屏幕外 */
    isbannerSee: boolean = false;
    /**广告实例 */
    adInstance: any = null;
    /**广告类型 */
    adType: AdTypes = AdTypes.None;
    /**是否加载成功,调用show的时候要先判断此开关 */
    isLoaded: boolean = false;
    /**广告id数组索引 */
    adsIndex: number = 0;
    /** 广告关闭后需不需要预加载 */
    neespreLoad: boolean = true;
    /**广告加载错误次数 */
    loadCount: number = 0;
    /**是否已经重新创建了 */
    iscxload: boolean = false;
    /**banner style 格式 */
    style: {
        width: number,      //广告宽度
        height: number,     //广告高度
        realWidth: number,  //广告真实宽度
        realHeight: number,  //广告真实高度
        top: number,           //广告顶部
        left: number          //广告左边     
    } = { width: 600, height: 100, realWidth: 600, realHeight: 100, top: 0, left: 0 }
    /**广告刷新时间，到时间就关闭重建刷新 */
    adTime: number = 30 * 1000;
    /**记录广告的展示时间， */
    showAdTime: number = 0;
    /**记录广告的创建时间， */
    creatTime: number = 0;
    /**位置下标 */
    posid: number = 0;

    /**
     * @param adsType 广告类型
     */
    constructor(adsType: AdTypes) {
        this.adType = adsType;
        this.ClearAds();
    }

    /** 清理广告 */
    ClearAds() {
        this.isLoaded = false;
        this.isShow = false;
        this.isShowing = false;
        this.loadCount = 0;
    }

    /** 设置广告 */
    setAds(showFunc: Function, closeFunc: Function, data?: any, caller?: any) {
        this._cbShow = showFunc;
        this._cbClose = closeFunc;
        this.data = data;
        this.caller = caller;
    }

    /** 广告是否显示成功回调 */
    AdShow(state, data?: any) {
        if (!this._cbShow) return;

        //console.log("显示视频广告显示成功回调");

        if (this.caller) {
            this._cbShow.call(this.caller, state, data);
        } else {
            this._cbShow(state, data);
        }

        if (state) {
            this.isShowing = true;
            this.isShow = true;
        } else {
            this._cbClose = null;
            this.isLoaded = false;
            this.isShowing = false;
            this.isShow = false;
        }

        this._cbShow = null;
    }

    /**广告关闭是否全部看完的回调 */
    AdClose(state, data?: any) {
        this.isLoaded = false;
        this.isShowing = false;
        this.isShow = false;

        if (!this._cbClose) return;

        if (this.caller) {
            this._cbClose.call(this.caller, state, data);
        } else {
            this._cbClose(state, data);
        }

        this._cbClose = null;
    }

    /**
     * 设置广告显示回调
     * @param showFunc 
     */
    SetShowCall(showFunc: Function) {
        this._cbShow = showFunc;
    }

    /**
     * 设置广告关闭回调函数
     * @param closeFunc 
     */
    SetCloseCall(closeFunc: Function) {
        this._cbClose = closeFunc;
    }

    /**
     * 广告加载结果
     * @param state 
     */
    AdLoad(state: boolean) {
        this.isLoaded = state;
    }

    /**
     * 删除广告实例
     * 返回是否消除广告实例成功
     */
    DestroyAd(): boolean {
        if (this.adInstance && typeof this.adInstance.destroy != 'undefined') {
            this.adInstance.destroy();
            this.adInstance = null;
            return true;
        }
        return false;
    }
}